package com.bankcomm.creditcard.redfriday.shop;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bankcomm.creditcard.redfriday.R;
import com.bankcomm.creditcard.redfriday.data.Shop;

public class ShopListAdapter extends BaseAdapter {

	private LayoutInflater layoutInflater;
	private List<Shop> list;

	public ShopListAdapter(Context context, List<Shop> list) {
		this.layoutInflater = LayoutInflater.from(context);
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.shoplist_item, null);
			viewHolder = new ViewHolder();
			viewHolder.nameTextView = (TextView) convertView
					.findViewById(R.id.shoplist_item_name);
			viewHolder.distanceTextView = (TextView) convertView
					.findViewById(R.id.shoplist_item_distance);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		Shop shop = list.get(position);
		viewHolder.nameTextView.setText(shop.marketTitle);
		String distanceStr = ((shop.distance / 1000) + "000").substring(0, 3);
		viewHolder.distanceTextView.setText("距离" + distanceStr + "公里");
		return convertView;
	}

	class ViewHolder {
		TextView nameTextView;
		TextView distanceTextView;
	}
}
